#ifndef _mathlib_h_
#define _mathlib_h_

#include <math.h>

#define PIOVER180 0.0174532925
#define PI        3.1415926535

namespace MathLib2d
{

    /*
    *  2d point.
    */
    public struct Point2d 
    {
	    float _x;
	    float _y;

	    Point2d(float x, float y) 
            : _x(x), 
              _y(y)
	    {

	    }
    };

    /*
    *  2d vector.
    */
    public struct Vector2d 
    {
        float _a;
        float _b;

        Vector2d(float a, float b) 
            : _a(a), 
              _b(b)
        {

        }

        //Ensures vector is non-zero
        bool isNonzero()
        {
            bool nonzero = false;
            if ( (_a != 0.0f) || (_b != 0.0f) )
            {
                nonzero = true;
            }
            return nonzero;
        }

        //Normalizes vector to a specified size
        void normalize(const float& size)
        {
            if (isNonzero() && (size != 0))
            {
                float length = sqrt(pow(_a, 2) + pow(_b, 2));
                if (length != 0)
                {
                    _a *= (size/length);
                    _b *= (size/length);
                }
            }
        }
    };

    /*
    *  2d scalar vector.
    */
    public struct VectorScalar2d : Vector2d 
    {
	    float _scalar;

	    VectorScalar2d(float x, float y, float scalar)
		    : Vector2d (x, y), 
              _scalar (scalar)
	    {

	    }
    };

    /*
    *  2d plane.
    *  Represented as ax + by + c = 0;
    */
    public struct Plane2d : Vector2d, Point2d
    {
        Plane2d(float x, float y, float a, float b) 
            : Point2d (x, y), 
              Vector2d (a, b)
        {

        }
    };

	/*
	*  Calculates the distance between a 2d point and a 2d plane.
	*/
    static float PointToPlaneDistance(const Point2d& point, 
                                      const Plane2d& plane);

	/*
	*  Rotate point X, Y around the origin's Z axis by
	*  a certain number of degrees.
    *  TODO: Consider using quaternions for speed.
	*/
	static void RotatePoint(float& x, float& y, const float& degrees);
};

#endif